package com.muyu.danmo.service.impl;


import com.muyu.common.core.utils.StringUtils;
import com.muyu.danmo.domain.DanmoPrintingPicture;
import com.muyu.danmo.domain.DanmoPrintingType;
import com.muyu.danmo.mapper.DanmoPrintingPictureMapper;
import com.muyu.danmo.service.IDanmoPrintingPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 印刷出版图片Service业务层处理
 *
 * @author yangle
 * @date 2024-10-15
 */
@Service
public class DanmoPrintingPictureServiceImpl implements IDanmoPrintingPictureService
{
    @Autowired
    private DanmoPrintingPictureMapper danmoPrintingPictureMapper;

    /**
     * 查询印刷出版图片
     *
     * @param id 印刷出版图片主键
     * @return 印刷出版图片
     */
    @Override
    public DanmoPrintingPicture selectDanmoPrintingPictureById(Long id)
    {
        return danmoPrintingPictureMapper.selectDanmoPrintingPictureById(id);
    }

    /**
     * 查询印刷出版图片列表
     *
     * @param danmoPrintingPicture 印刷出版图片
     * @return 印刷出版图片
     */
    @Override
    public List<DanmoPrintingPicture> selectDanmoPrintingPictureList(DanmoPrintingPicture danmoPrintingPicture)
    {
        return danmoPrintingPictureMapper.selectDanmoPrintingPictureList(danmoPrintingPicture);
    }

    /**
     * 新增印刷出版图片
     *
     * @param danmoPrintingPicture 印刷出版图片
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDanmoPrintingPicture(DanmoPrintingPicture danmoPrintingPicture)
    {
        int rows = danmoPrintingPictureMapper.insertDanmoPrintingPicture(danmoPrintingPicture);
        insertDanmoPrintingType(danmoPrintingPicture);
        return rows;
    }

    /**
     * 修改印刷出版图片
     *
     * @param danmoPrintingPicture 印刷出版图片
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDanmoPrintingPicture(DanmoPrintingPicture danmoPrintingPicture)
    {
        danmoPrintingPictureMapper.deleteDanmoPrintingTypeById(danmoPrintingPicture.getId());
        insertDanmoPrintingType(danmoPrintingPicture);
        return danmoPrintingPictureMapper.updateDanmoPrintingPicture(danmoPrintingPicture);
    }

    /**
     * 批量删除印刷出版图片
     *
     * @param ids 需要删除的印刷出版图片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoPrintingPictureByIds(Long[] ids)
    {
        danmoPrintingPictureMapper.deleteDanmoPrintingTypeByIds(ids);
        return danmoPrintingPictureMapper.deleteDanmoPrintingPictureByIds(ids);
    }

    /**
     * 删除印刷出版图片信息
     *
     * @param id 印刷出版图片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoPrintingPictureById(Long id)
    {
        danmoPrintingPictureMapper.deleteDanmoPrintingTypeById(id);
        return danmoPrintingPictureMapper.deleteDanmoPrintingPictureById(id);
    }

    /**
     * 新增印刷出版类型信息
     *
     * @param danmoPrintingPicture 印刷出版图片对象
     */
    public void insertDanmoPrintingType(DanmoPrintingPicture danmoPrintingPicture)
    {
        List<DanmoPrintingType> danmoPrintingTypeList = danmoPrintingPicture.getDanmoPrintingTypeList();
        Long id = danmoPrintingPicture.getId();
        if (StringUtils.isNotNull(danmoPrintingTypeList))
        {
            List<DanmoPrintingType> list = new ArrayList<DanmoPrintingType>();
            for (DanmoPrintingType danmoPrintingType : danmoPrintingTypeList)
            {
                danmoPrintingType.setId(id);
                list.add(danmoPrintingType);
            }
            if (list.size() > 0)
            {
                danmoPrintingPictureMapper.batchDanmoPrintingType(list);
            }
        }
    }
}
